#delimit ;

*parameters;
local nrows=22;
local exch=2288; //Exchange rate TSH per USD

*Calculate owner statistics----------------------------------------------------;
use "${sdir}/Leaders_and_Owners_cln.dta", clear;
*Keep one observation per plot;
bysort shina_id plot_id: gen count=_n;
keep if count==1;
drop count;


*Only keep participating owners;
drop if owner_observed==0;

local c=1;

*Share invoices with one owner only;
gen 	r1_c`c'_v=(s0_q_1==1);
local 	r1_c`c'_lab="Sole ownership";

*Share invoices with sole female owner;
gen 	r2_c`c'_v=(s0_q_1==1 & s1_q_1_1==1);
local 	r2_c`c'_lab="Sole ownership and female";

*Share invoices with a young owner;
gen 	r3_c`c'_v=(s1_q_2_1<=1 | (s1_q_2_2<=1 & s0_q_1==2));
local 	r3_c`c'_lab="Under 40 years old";

*Share invoices with an old owner;
gen 	r4_c`c'_v=(s1_q_2_1==4 | (s1_q_2_2==4 & s0_q_1==2));
local 	r4_c`c'_lab="Over 60 years old";

*Share invoices primary or less;
gen 	r5_c`c'_v=(s1_q_5_1<=2 | (s1_q_5_2<=2 & s0_q_1==2));
local 	r5_c`c'_lab="Educ. primary or less";

*Share invoices more than secondary;
gen 	r6_c`c'_v=(s1_q_5_1>=5 | (s1_q_5_2>=5 & s0_q_1==2));
local 	r6_c`c'_lab="Educ. above secondary";

*Share invoices monthly income below 100,000TSh;
gen 	r7_c`c'_v=(s1_q_6_1<=1 | (s1_q_6_2<=1 & s0_q_1==2));
**local 	r7_c`c'_lab="Monthly income \$<\$ 100,000TSh";
local 	r7_c`c'_lab="Monthly HH income \$<\$ 45";

*Share invoices monthly income above 300,000TSh;
gen 	r8_c`c'_v=(s1_q_6_1>=5 | (s1_q_6_2>=5 & s0_q_1==2));
*local 	r8_c`c'_lab="Monthly income \$>\$ 300,000TSh";
local 	r8_c`c'_lab="Monthly HH income \$>\$ 130";

*Average CRO quiz score;
gen 	r9_c`c'_v=s3_quiz_score;
local 	r9_c`c'_lab="Avg. CRO quiz score";

*----differing characteristics from leaders--------;

*Share invoices with no children; 
gen 	r10_c`c'_v=(s1_q_4_1==0 | (s1_q_4_2==0 & s0_q_1==2));
local 	r10_c`c'_lab="No children";

*Share invoices with over 4 children; 
gen 	r11_c`c'_v=(s1_q_4_1>4 | (s1_q_4_2<4 & s0_q_1==2));
local 	r11_c`c'_lab="Over 4 children";

*Share invoices with absentee owner;
gen 	r12_c`c'_v=(s1_q_7_1!=1 | (s1_q_7_2!=1 & s0_q_1==2));
local 	r12_c`c'_lab="Absentee Owner";

*Share acquired in last 6 years;
gen 	r13_c`c'_v=(s1_q_8_1<=1 & missing(s1_q_8_1)==0);
local 	r13_c`c'_lab="Acquired in last 6 years";

*Share acquired over 19 years ago;
gen 	r14_c`c'_v=(s1_q_8_1>=3 & missing(s1_q_8_1)==0);
local 	r14_c`c'_lab="Acquired over 19 years";

*Share acquired by purchase;
gen 	r15_c`c'_v=(s1_q_8_2==1);
local 	r15_c`c'_lab="Acquired by purchase";

*Share with sale certificate;
gen 	r16_c`c'_v=(s1_q_8_3==1);
local 	r16_c`c'_lab="Has sale certificate";

*Share owns another plot;
gen 	r17_c`c'_v=(s1_q_9_1>=1 & missing(s1_q_9_1)==0);
local 	r17_c`c'_lab="Owns another plot";

*Share owns another surveyed plot;
gen 	r18_c`c'_v=(s1_q_9_2>=1 & missing(s1_q_9_2)==0);
local 	r18_c`c'_lab="Owns another surveyed plot";

*Share owns another titled plot;
gen 	r19_c`c'_v=(s1_q_9_3>=1 & missing(s1_q_9_3)==0);
local 	r19_c`c'_lab="Owns another titled plot";

*Average invoice value;
gen 	r20_c`c'_v=(invoice_value2/`exch');
local 	r20_c`c'_lab="Avg. invoice value (USD)";

*Average plot area;
gen 	r21_c`c'_v=(lot_area_inv_obl);
local 	r21_c`c'_lab="Avg. plot area (sqm)";

*Average BDM bid;
gen 	r22_c`c'_v=(s9_q_1/`exch');
local 	r22_c`c'_lab="Avg. BDM bid (USD)";

*----calculate statistics-----------------------------;
forvalues r=1/`nrows'
	{;
	capture desc r`r'_c`c'_v;
	if _rc==0
		{;
		sum r`r'_c`c'_v;
		*MEAN;
		if r(mean)<1
			{;local r`r'_c`c': di %03.2f r(mean);};
		else if r(mean)<10
			{;local r`r'_c`c': di %3.1f r(mean);};
		else if r(mean)<100
			{;local r`r'_c`c': di %3.0f r(mean);};
		else if r(mean)<1000
			{;local r`r'_c`c': di %4.0f r(mean);};
		*SE;
		if r(sd)/sqrt(r(N))<1
			{;local r`r'_c`c'_se: di %04.3f r(sd)/sqrt(r(N));
			local r`r'_c`c'_se="(`r`r'_c`c'_se')";};
		else if r(sd)/sqrt(r(N))<10
			{;local r`r'_c`c'_se: di %3.2f r(sd)/sqrt(r(N));
			local r`r'_c`c'_se="(`r`r'_c`c'_se')";};
		else if r(sd)/sqrt(r(N))<100
			{;local r`r'_c`c'_se: di %3.1f r(sd)/sqrt(r(N));
			local r`r'_c`c'_se="(`r`r'_c`c'_se')";};
		};
	};
*Count invoices;
local c1_N: di %3.0f _N;


*Calculate leader statistics----------------------------------------------------;
use "${sdir}/Leaders_and_Owners_cln.dta", clear;

*Create counts of network variables-----;
gen q10_dum=(q10_ldr==1|q10_ldr==2|q10_ldr==3|q10_ldr==4) 	if missing(q10_ldr)==0; //Indicator if owner known at all
gen q11_dum=(q11_ldr==1|q11_ldr==2|q11_ldr==3|q11_ldr==4) 	if missing(q11_ldr)==0; //Indicator if owner used services at all
gen q12_dum=(q12_ldr==1) 									if missing(q12_ldr)==0; //Indicator if owner is a family member
gen q13_dum=(q13_ldr==1) 									if missing(q13_ldr)==0; //Indicator if owner is a close friend
gen q14_dum=(q14_ldr==1) 									if missing(q14_ldr)==0; //Indicator if owner is of same religious affiliation
gen q15_dum=(q15_ldr==1) 									if missing(q15_ldr)==0; //Indicator if owner is highly esteemed

foreach v in q10 q11 q12 q13 q14 q15
	{;
	bysort shina_id leader_id: egen `v'_tot=total(`v'_dum);
	drop `v'_dum;
	};
*---------------------------------------;

*Keep one obs per leader;
bysort shina_id leader_id: gen count=_n;
keep if count==1;
drop count;

*Make treatment indicators;
gen stakescontrol=0 if trt_grp==0;
replace stakescontrol=1 if trt_grp==2;
gen incentivescontrol=0 if trt_grp==0;
replace incentivescontrol=1 if trt_grp==1;

local c=2;
*blank;
local r1_c2_lab="";

*Share female leaders;
gen 	r2_c`c'_v=(gender==1);
local 	r2_c`c'_lab="Female";

*Share young leaders;
gen 	r3_c`c'_v=(age<=1 & missing(age)==0);
local 	r3_c`c'_lab="Under 40 years old";

*Share old leaders;
gen 	r4_c`c'_v=(age==4);
local 	r4_c`c'_lab="Over 60 years old";

*Share primary or less;
gen 	r5_c`c'_v=(q8_1<=2 & missing(q8_1)==0);
local 	r5_c`c'_lab="Educ. primary or less";

*Share more than secondary;
gen 	r6_c`c'_v=(q8_1>=5 & missing(q8_1)==0);
local 	r6_c`c'_lab="Educ. above secondary";

*Share invoices monthly income below 100,000TSh;
gen 	r7_c`c'_v=(q9_1<=1 & missing(q9_1)==0);
**local 	r7_c`c'_lab="Monthly income \$<\$ 100,000TSh";
local 	r7_c`c'_lab="Monthly HH income \$<\$ 45";

*Share invoices monthly income above 300,000TSh;
gen 	r8_c`c'_v=(q9_1>=5 & missing(q9_1)==0);
**local 	r8_c`c'_lab="Monthly income \$>\$ 300,000TSh";
local 	r8_c`c'_lab="Monthly HH income \$>\$ 130";

*Average CRO quiz score;
gen 	r9_c`c'_v=(quiz_score);
local 	r9_c`c'_lab="Avg. CRO quiz score";

*----differing characteristics from leaders--------;
*Share opposition party;
gen 	r10_c`c'_v=(q1_1==1);
local 	r10_c`c'_lab="Opposition party";

*Share assistant leader;
gen 	r11_c`c'_v=(q1_1==2);
local 	r11_c`c'_lab="Assistant leader";

*Share plot owner;
gen 	r12_c`c'_v=(q3_1==0);
local 	r12_c`c'_lab="Owns their home plot";

*Share settled in last 6 years;
gen 	r13_c`c'_v=(q5_1==0|q5_1==1);
local 	r13_c`c'_lab="Settled in last 6 years";

*Share acquired over 19 years ago;
gen 	r14_c`c'_v=(q5_1>=3 & missing(q5_1)==0);
local 	r14_c`c'_lab="Settled over 19 years";

*Share home plot surveyed;
gen 	r15_c`c'_v=(q4_1==0);
local 	r15_c`c'_lab="Home plot surveyed";

*Number of 15 owners known at all;
gen 	r16_c`c'_v=q10_tot;
local 	r16_c`c'_lab="Count of 15 owners known at all";

*Number of 15 owners used services at all;
gen 	r17_c`c'_v=q11_tot;
local 	r17_c`c'_lab="Count of 15 owners use services at all";

*Number of 15 owners family members;
gen 	r18_c`c'_v=q12_tot;
local 	r18_c`c'_lab="Count of 15 owners family members";

*Number of 15 owners close friend;
gen 	r19_c`c'_v=q13_tot;
local 	r19_c`c'_lab="Count of 15 owners close friends";

*Number of 15 owners close friend;
gen 	r20_c`c'_v=q14_tot;
local 	r20_c`c'_lab="Count of 15 owners religious affiliation";

*Number of 15 owners high esteemed;
gen 	r21_c`c'_v=q15_tot;
local 	r21_c`c'_lab="Count of 15 owners highly esteemed";


*----calculate statistics-----------------------------;
forvalues r=1/`nrows'
	{;
	capture desc r`r'_c`c'_v;
	if _rc==0
		{;
		sum r`r'_c`c'_v;
		*MEAN;
		if r(mean)<1
			{;local r`r'_c`c': di %03.2f r(mean);};
		else if r(mean)<10
			{;local r`r'_c`c': di %3.1f r(mean);};
		else if r(mean)<100
			{;local r`r'_c`c': di %3.0f r(mean);};
		else if r(mean)<1000
			{;local r`r'_c`c': di %4.0f r(mean);};
		*SE;
		if r(sd)/sqrt(r(N))<1
			{;local r`r'_c`c'_se: di %04.3f r(sd)/sqrt(r(N));
			local r`r'_c`c'_se="(`r`r'_c`c'_se')";};
		else if r(sd)/sqrt(r(N))<10
			{;local r`r'_c`c'_se: di %3.2f r(sd)/sqrt(r(N));
			local r`r'_c`c'_se="(`r`r'_c`c'_se')";};
		else if r(sd)/sqrt(r(N))<100
			{;local r`r'_c`c'_se: di %3.1f r(sd)/sqrt(r(N));
			local r`r'_c`c'_se="(`r`r'_c`c'_se')";};
		
		*Test difference stakes and control----;
		ttest  r`r'_c`c'_v, by(stakescontrol);
		if  r(p)<0.01{;local r`r'_c3_star="\$^{***}\$";};
		else if r(p)<0.05{;local r`r'_c3_star="\$^{**}\$";};
		else if r(p)<0.1{;local r`r'_c3_star="\$^*\$";};
		else{;local r`r'_c3_star="";};
		*MEAN;
		local diff=r(mu_2)-r(mu_1);
		if `diff'<1
			{;local r`r'_c3: di %03.2f `diff';
			local local r`r'_c3="`local r`r'_c3'";};
		else if `diff'<10
			{;local r`r'_c3: di %3.1f `diff';
			local local r`r'_c3="`local r`r'_c3'";};
		else if `diff'<100
			{;local r`r'_c3: di %3.0f `diff';
			local local r`r'_c3="`local r`r'_c3'";};
		else if `diff'<1000
			{;local r`r'_c3: di %4.0f `diff';
			local local r`r'_c3="`local r`r'_c3'";};
		*SE;
		local se=r(se);
		if `se'<1
			{;local r`r'_c3_se: di %04.3f `se';
			local r`r'_c3_se="(`r`r'_c3_se')";};
		else if `se'<10
			{;local r`r'_c3_se: di %3.2f `se';
			local r`r'_c3_se="(`r`r'_c3_se')";};
		else if `se'<100
			{;local r`r'_c3_se: di %3.1f `se';
			local r`r'_c3_se="(`r`r'_c3_se')";};	
			
		*Test difference incentives and control----;
		ttest  r`r'_c`c'_v, by(incentivescontrol);
		if  `r(p)'<0.01{;local r`r'_c4_star="\$^{***}\$";};
		else if `r(p)'<0.05{;local r`r'_c4_star="\$^{**}\$";};
		else if `r(p)'<0.1{;local r`r'_c4_star="\$^*\$";};
		else{;local r`r'_c4_star="";};
		
		*MEAN;
		local diff=r(mu_2)-r(mu_1);
		if `diff'<1
			{;local r`r'_c4: di %03.2f `diff';
			local local r`r'_c4="`local r`r'_c4'";};
		else if `diff'<10
			{;local r`r'_c4: di %3.1f `diff';
			local local r`r'_c4="`local r`r'_c4'";};
		else if `diff'<100
			{;local r`r'_c4: di %3.0f `diff';
			local local r`r'_c4="`local r`r'_c4'";};
		else if `diff'<1000
			{;local r`r'_c4: di %4.0f `diff';
			local local r`r'_c4="`local r`r'_c4'";};
		*SE;
		local se=r(se);
		if `se'<1
			{;local r`r'_c4_se: di %04.3f `se';
			local r`r'_c4_se="(`r`r'_c4_se')";};
		else if `se'<10
			{;local r`r'_c4_se: di %3.2f `se';
			local r`r'_c4_se="(`r`r'_c4_se')";};
		else if `se'<100
			{;local r`r'_c4_se: di %3.1f `se';
			local r`r'_c4_se="(`r`r'_c4_se')";};
		};
	};
*Count invoices;
local c2_N: di %3.0f _N;



*----Write to table------------------------------------------------------------;
*Write to table;
file open tex using "tables/t_descriptives.tex", write text replace;
file write tex "\begin{center}" _n;
file write tex "\begin{tabular}{lC{2cm}lC{2cm}C{2cm}C{2cm}} \hline \hline \\" _n;
file write tex " \multicolumn{2}{c}{Plots} 	&  \multicolumn{4}{c}{Leaders} \\\cmidrule(r){1-2}\cmidrule(r){3-6}" _n;
file write tex " & (1) 	& 	& (2)		& (3)			& (4) 			\\[6pt]" _n;
file write tex " & Mean & 	& Mean		& Diff			& Diff 			\\" _n;
file write tex " &  	& 	& 	& Stakes-Contr	& Incent-Contr 	\\\cmidrule(r){2-2}\cmidrule(r){4-4}\cmidrule(r){5-5}\cmidrule(r){6-6}" _n;
forvalues r=1/`nrows'
	{;
	file write tex "\multirow[t]{2}{3cm}{`r`r'_c1_lab'} & `r`r'_c1' 	& \multirow[t]{2}{3cm}{`r`r'_c2_lab'} 	& `r`r'_c2' 	& `r`r'_c3'`r`r'_c3_star' 	& `r`r'_c4'`r`r'_c4_star' \\" _n;
	file write tex " & `r`r'_c1_se' 	& 	& `r`r'_c2_se' & `r`r'_c3_se' & `r`r'_c4_se' \\[6pt]" _n;};
file write tex "N & `c1_N'  	& N	& `c2_N' & &\\" _n;
file write tex "\hline\multicolumn{6}{l}{\begin{scriptsize}\$^*p\leq0.10\$, \$^{**}p\leq0.05\$, \$^{***}p\leq0.01\$  for difference=0 t-test\end{scriptsize}}\\" _n;
file write tex "\multicolumn{6}{l}{\begin{scriptsize}Standard errors in parentheses\end{scriptsize}}" _n;
file write tex "\end{tabular}" _n;
file write tex "\end{center}" _n;
file close tex;

*------------------------------------------------------------------------------;

*END--------
clear;
